//
//  ProductList_B_ViewController.m
//  NewApp
//
//  Created by xueyan liu on 6/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ProductList_BCD_ViewController.h"
#import "CustomTableCellB.h"
#import "CustomTableCellC.h"
#import "CustomTableCellD.h"
#import "ProductData.h"
#import "SystemInlineViewController.h"

@interface ProductList_BCD_ViewController ()

@end

@implementation ProductList_BCD_ViewController
@synthesize dataArray;
@synthesize listStyle;
@synthesize titleText;
@synthesize bHideBack;
@synthesize productTable;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (bHideBack) {
          self.navigationItem.hidesBackButton = YES;  
    }else {
        self.navigationItem.hidesBackButton =NO;
    }

    self.titleLabel.text = titleText;
    //productTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 26, 320, ) style:UITableViewStylePlain];
    productTable = [[UITableView alloc] init];
    productTable.delegate = self;
    productTable.dataSource = self;
    
    [productTable setBackgroundColor:[UIColor grayColor]];
    [productTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 26, 320, 480)];
    [self.view addSubview:scrollView];
    [scrollView addSubview:productTable];

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:NO];
   // [self.navigationController popToRootViewControllerAnimated:NO];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([listStyle isEqualToString:@"D"]) {
    
        return 40.0;
    }else{
       
        return 90.0;    
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    [tableView setFrame:CGRectMake(0, 0, 320, [dataArray count]*90)];
    [scrollView setContentSize:CGSizeMake(320, [dataArray count]*90+140)];
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        ProductEntity *product = [dataArray objectAtIndex:indexPath.row];
        
        [cell.backgroundView setFrame:CGRectMake(0, 0, 320, 90)];
        cell.backgroundColor=[UIColor clearColor];
        if (indexPath.row % 2 == 0 ) {
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_1.png"]]; 
        }else {
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_2.png"]];
        }
        
        UITableViewCell *celltmp;
        if([listStyle isEqualToString:@"B"]){
            celltmp = [[CustomTableCellB alloc] init];
            [(CustomTableCellB*)celltmp setContentWithImage:product.img title:product.title subtitle:product.sd content:product.info];
        }else if([listStyle isEqualToString:@"C"]) {
            celltmp = [[CustomTableCellC alloc] init];
            [(CustomTableCellC*)celltmp setContentWithImage:product.img title:product.title subtitle:product.sd content:product.info];
        }else if([listStyle isEqualToString:@"D"]) {
            celltmp = [[CustomTableCellD alloc] init];
            [(CustomTableCellD*)celltmp setContentWithTitle:product.title subTitle:product.sd exContent:@""];
        }
        [cell addSubview:celltmp];

    }
    // Configure the cell...
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

    SystemInlineViewController *vctrl = [[SystemInlineViewController alloc] init];
    vctrl.ID = ((ProductEntity *)[dataArray objectAtIndex:indexPath.row]).ID;
    self.title = @"返回";
    self.view = vctrl.view;
    [self.navigationController pushViewController:vctrl animated:YES];
    //[vctrl release];
}

@end
